package com.ygq.Impl;

import DO.JudgeQuestionDO;
import DTO.AddJudgeDTO;
import DTO.PageJudgeQuestionDTO;
import DTO.UpdateJudgeQuestionDTO;
import Exceptions.QuestionNumberExistedException;
import VO.JudgeQuestionVO;
import com.ygq.JudgeQuestionService;
import mapper.JudgeQuestionMapper;
import mapper.WorkJudgeMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import result.PageResult;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

@Service
public class JudgeQuestionServiceImpl implements JudgeQuestionService {

    @Autowired
    private JudgeQuestionMapper judgeQuestionMapper;

    @Autowired
    private WorkJudgeMapper workJudgeMapper;

    /**
     * 分页查询
     * @return
     */
    public PageResult page(PageJudgeQuestionDTO pageJudgeQuestionDTO) {

        PageResult pageResult = new PageResult();
        // 第一次查询 查询总数
        Integer total = judgeQuestionMapper.getTotal(pageJudgeQuestionDTO);

        // 第二次查询 查询分页数据
        /*
         * 这里将getCurrentPage作为 (currentPage - 1) * pageSize;
         */
        pageJudgeQuestionDTO.setCurrentPage( (pageJudgeQuestionDTO.getCurrentPage() - 1) * pageJudgeQuestionDTO.getPageSize());
        List<JudgeQuestionVO> list = judgeQuestionMapper.page(pageJudgeQuestionDTO);
        pageResult.setTotal(total);
        pageResult.setRecords(list);
        return pageResult;
    }

    /**
     * 添加题目
     * @param addJudgeDTO
     */
    public void addQuestion(AddJudgeDTO addJudgeDTO) {
        // 第一步 判断编号是否重复，如果重复，则 报异常 编号已经重复
        String questionNumber = addJudgeDTO.getQuestionNumber();
        List<String> list=judgeQuestionMapper.getquestionNumber();
        for (int i = 0; i < list.size(); i++) {
            // 抛出异常，编号重复
            if (list.get(i).equals(questionNumber)) throw new QuestionNumberExistedException();
        }
        JudgeQuestionDO judgeQuestionDO = new JudgeQuestionDO();
        BeanUtils.copyProperties(addJudgeDTO,judgeQuestionDO);
        judgeQuestionDO.setCreateTime(LocalDateTime.now());
        // 调用添加mapper，添加数据
        judgeQuestionMapper.addQuestion(judgeQuestionDO);
    }

    /**
     * 删除题目
     * @param id
     */
    public Integer deleteQuestion(Integer id) {
        Integer number = workJudgeMapper.getIsrelevancyWork(id);
        if (number>0){
            return 1;
        }
        judgeQuestionMapper.deleteById(id);
        return 0;
    }

    /**
     * 修改题目
     * @param updateJudgeQuestionDTO
     */
    public Integer updateJudge(UpdateJudgeQuestionDTO updateJudgeQuestionDTO) {
        // 第一步，先要判断答案是否发生更改，
        // 第二步，如果答案发生更改，则要判断这个试题是否关联试卷，如果关联，则报出 code 为 512 的 答案因为关联试卷不能修改 的异常
        // 第三步，调用mapper，更改数据
        Integer answerById = judgeQuestionMapper.getAnswerById(updateJudgeQuestionDTO.getId());
        if (!Objects.equals(answerById, updateJudgeQuestionDTO.getAnswer())){// 答案不相同，不能修改
            Integer number = workJudgeMapper.getIsrelevancyWork(updateJudgeQuestionDTO.getId());
            if (number>0){
                return 1;
            }
        }
        judgeQuestionMapper.updateById(updateJudgeQuestionDTO);
        return 0;
    }
}
